package controller

import (
	_ "fmt"
	"golang_start/app/api/service"
	"golang_start/common"
	"strings"

	"github.com/gin-gonic/gin"
	/*{%service_package%}*/)

var CodeContentserver = &service.CodeContent{}

func init() {

}

type CodeContent struct {
}

/*添加*/
func (model *CodeContent) Add_(c *gin.Context) {

	var data service.CodeContent
	c.Bind(&data)
	id, err := CodeContentserver.Add_(c, &data)
	if err != nil {
		c.JSON(common.FAIL_CODE, gin.H{
			"code": common.FAIL_CODE,
			"msg":  "错误" + string(err.Error()),
		})
		return
	}
	c.JSON(common.SUCCESS_CODE, gin.H{
		"code": common.SUCCESS_CODE,
		"msg":  "成功",
		"data": id,
	})
}

/*删除*/
func (model *CodeContent) Delete_(c *gin.Context) {

	deleteids := &service.CodeContentDeleteids{}
	c.Bind(&deleteids)
	_, err := CodeContentserver.Delete_(c, deleteids)

	if err != nil {
		c.JSON(common.FAIL_CODE, gin.H{
			"code": common.FAIL_CODE,
			"msg":  "操作失败:" + string(err.Error()),
		})
		return
	}
	c.JSON(common.SUCCESS_CODE, gin.H{
		"code": common.SUCCESS_CODE,
		"msg":  "操作成功",
	})
	return
}

/*修改*/
func (model *CodeContent) Update_(c *gin.Context) {
	update_data := &service.CodeContent{}
	c.Bind(update_data)
	_, err := CodeContentserver.Update_(c, update_data)

	if err != nil {
		c.JSON(common.FAIL_CODE, gin.H{
			"code": common.FAIL_CODE,
			"msg":  "操作失败:" + string(err.Error()),
		})
		return
	}
	c.JSON(common.SUCCESS_CODE, gin.H{
		"code": common.SUCCESS_CODE,
		"msg":  "操作成功",
	})
	return

}

/*查询(单条)*/
func (model *CodeContent) Detail_(c *gin.Context) {

	var data = &service.CodeContent{}
	c.Bind(&data)
	data, err := CodeContentserver.Detail_(c, data)

	if err != nil {
		c.JSON(common.FAIL_CODE, gin.H{
			"code": common.FAIL_CODE,
			"msg":  "操作失败:" + string(err.Error()),
		})
		return
	}
	c.JSON(common.SUCCESS_CODE, gin.H{
		"code": common.SUCCESS_CODE,
		"msg":  "操作成功",
		"data": data,
	})
}

func (model *CodeContent) FindByIds(c *gin.Context) {

	data, err := CodeContentserver.FindByIds(c, strings.Split(c.Query("id"), ","))

	if err != nil {
		c.JSON(common.FAIL_CODE, gin.H{
			"code": common.FAIL_CODE,
			"msg":  "操作失败:" + string(err.Error()),
		})
		return
	}
	c.JSON(common.SUCCESS_CODE, gin.H{
		"code": common.SUCCESS_CODE,
		"msg":  "操作成功",
		"data": data,
	})
}

/*查询列表*/
func (model *CodeContent) List_(c *gin.Context) {
	param := make(map[string]string)
	Psize := c.DefaultQuery("psize", "10")
	Pindex := c.DefaultQuery("pindex", "1")
	Order_column := c.DefaultQuery("order", "id")
	Order_sort := c.DefaultQuery("order_sort", "desc")
	export_json := c.DefaultQuery("export_json", "")
	param["psize"] = Psize
	param["pindex"] = Pindex
	param["order"] = Order_column
	param["order_sort"] = Order_sort
	param["export_json"] = export_json
	data, count, err := CodeContentserver.List_(c, param)
	if err != nil {
		c.JSON(common.FAIL_CODE, gin.H{
			"code": common.FAIL_CODE,
			"msg":  "失败:" + string(err.Error()),
		})
		return
	}
	c.JSON(common.SUCCESS_CODE, gin.H{
		"code":  common.SUCCESS_CODE,
		"msg":   "成功",
		"data":  data,
		"count": count,
	})
}
